using System;
using System.Collections.Generic;
using System.Text;
using Madason.GenUtils;
using System.IO;
using System.Xml;
using System.Diagnostics;

namespace Madason.GeneratorTests
{
	public class Program
	{
		const string DIFF_PROGRAMM_ENV_VARIABLE = "MADASON_DIFF";

		/// <summary>
		/// show diff
		/// </summary>
		/// <param name="orgFile"></param>
		/// <param name="genFile"></param>
		public static void ShowDiff(FileInfo orgFile, FileInfo genFile)
		{
			string diffProg = System.Environment.GetEnvironmentVariable(DIFF_PROGRAMM_ENV_VARIABLE);
			if (diffProg == null)
			{

				Console.WriteLine("if you set a env variable " + DIFF_PROGRAMM_ENV_VARIABLE + " to your diff programm, we'll call the diff prog");
				return;
			}

			System.Diagnostics.Process.Start(diffProg, orgFile.FullName + " " + genFile.FullName);
		}



		/// <summary>
		/// Compare two files
		/// </summary>
		/// <param name="orgDir"></param>
		/// <param name="genDir"></param>
		/// <param name="errors"></param>
		public static void Compare(DirectoryInfo orgDir, DirectoryInfo genDir, List<string> errors)
		{
			foreach (FileInfo orgFile in orgDir.GetFiles())
			{
				FileInfo genFile = new FileInfo(genDir.FullName + "\\" + orgFile.Name);
				if (!genFile.Exists)
				{
					errors.Add("missing generated file " + genFile.FullName);
					continue;
				}


				string orgContent = null;
				using (StreamReader reader = new StreamReader(orgFile.FullName))
				{
					orgContent = reader.ReadToEnd();
				}
				string genContent = null;
				using (StreamReader reader = new StreamReader(genFile.FullName))
				{
					genContent = reader.ReadToEnd();
				}

				if (orgContent.Trim() != genContent.Trim())
				{
					errors.Add("file " + orgFile.Name + " has faults");
					ShowDiff(orgFile, genFile);
				}
			}
		}



		/// <summary>
		/// main programm
		/// </summary>
		/// <param name="args"></param>
		public static void Main(string[] args)
		{
			XmlDocument testprogram = new XmlDocument();
			testprogram.Load("Madason.GeneratorTests\\testprogram.xml");

			List<string> errors = new List<string>();
			foreach (XmlNode testNode in testprogram.SelectNodes("//test"))
			{
				string name = testNode.Attributes["name"].Value;
				try
				{
					string execution = testNode.SelectSingleNode("execution").InnerText;
					string arguments = testNode.SelectSingleNode("arguments").InnerText;
					string orgDir = testNode.SelectSingleNode("orgDir").InnerText;
					string genDir = testNode.SelectSingleNode("genDir").InnerText;

					Process madason = System.Diagnostics.Process.Start(execution, arguments);
					madason.WaitForExit();

					Compare(new DirectoryInfo(orgDir), new DirectoryInfo(genDir), errors);
				}
				catch (Exception e)
				{
					Console.WriteLine("error in test '" + name + "':" + e.ToString());
					System.Environment.Exit(-1);
				}
			}

			foreach (string error in errors)
			{
				Console.WriteLine(error);
			}
			if (errors.Count > 0) System.Environment.Exit(-1);
		}
	}
}
